package com.ping.study.controller;



import com.ping.study.model.dto.addUrls;
import com.ping.study.model.vo.live.LiveUrl;
import com.ping.study.pojo.Games;
import com.ping.study.service.GamesService;
import com.ping.study.service.UrlsService;
import com.ping.study.utils.NbaApi;

import lombok.Data;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RequestMapping("/api")
@RestController
@Data
@Slf4j
//@CrossOrigin(origins = "http://nba.new9.me")
@CrossOrigin
public class NbaController {

    @Autowired
    private NbaApi nbaApi;
    @Autowired
    private GamesService gamesService;
    @Autowired
    private UrlsService urlsService;

    //添加定时任务，每日凌晨0点执行一次
    // 每天 00:00:00 执行
    @Scheduled(cron = "0 0 0 * * ?")
    @RequestMapping("/add")
    public List<Games> addGames() {
        //先删除数据库中所有赛程
        log.info("执行定时方法删除数据库中所有赛程");
        gamesService.deleteAllGames();
        //再删除数据库中所有直播链接
        log.info("执行定时方法删除数据库中所有直播链接");
        urlsService.deleteAllUrls();
        log.info("执行定时方法添加当天赛程");
        return nbaApi.addGames();
    }
    @RequestMapping("/games")
    public List<Games> getGames() {
        log.info("获取所有赛程");
        return gamesService.getGames();
    }

    @RequestMapping("/urls")
    public List<HashMap<String, List<LiveUrl>>> getUrls() {
        log.info("获取所有赛程直播链接");
        return urlsService.getUrls();
    }

    @RequestMapping("/go")
    public Boolean go(@RequestParam("pwd") String pwd) {
        return pwd.equals("inspur123");
    }
    @RequestMapping(value = "/addUrls",method = RequestMethod.POST)
    public void addUrls(@RequestBody addUrls addUrls) {
        log.info("添加所有赛程直播链接");
        urlsService.addUrls(addUrls);
    }

    //删除直播链接根据id
    @RequestMapping("/delete/{id}")
    public void deleteUrlById(@PathVariable("id") Integer id){
        urlsService.deleteUrlById(id);
    }
}
